package org.minuframework.file;

import java.io.File;
import java.util.Properties;

import org.minuframework.utils.LangUtils;

public class PropertiesFile {
    
    private Properties properties;
    
    public PropertiesFile(String fileName) {
        this.properties = FileUtils.load(fileName);
    }
    
    public PropertiesFile(String fileName, String encoding) {
        this.properties = FileUtils.load(fileName, encoding);
    }
    
    public PropertiesFile(File file) {
        this.properties = FileUtils.load(file);
    }
    
    public PropertiesFile(File file, String encoding) {
        this.properties = FileUtils.load(file, encoding);
    }
    
    public PropertiesFile(Properties properties) {
        this.properties = properties;
    }
    
    public PropertiesFile append(PropertiesFile propertiesFile) {
        if (this.properties == null) {
            this.properties = propertiesFile.getProperties();
        } else {
            this.properties.putAll(propertiesFile.getProperties());
        }
        return this;
    }
    
    public PropertiesFile append(Properties properties) {
        return append(new PropertiesFile(properties));
    }
    
    public PropertiesFile append(String fileName) {
        return append(new PropertiesFile(fileName));
    }
    
    public PropertiesFile append(String fileName, String encoding) {
        return append(new PropertiesFile(fileName, encoding));
    }
    
    public PropertiesFile append(File file) {
        return append(new PropertiesFile(file));
    }
    
    public PropertiesFile append(File file, String encoding) {
        return append(new PropertiesFile(file, encoding));
    }
    
    public PropertiesFile setProperty(String key, String value) {
        properties.setProperty(key, value);
        return this;
    }
    
    public Object get(Object key) {
        return properties.get(key);
    }
    
    public Object get(String key) {
        return properties.get(key);
    }
    
    public String getProperty(String key) {
        return properties.getProperty(key);
    }
    
    public int getInt(String key) {
        String value = properties.getProperty(key);
        if (value != null) { return LangUtils.parseInt(value); }
        return 0;
    }
    
    public long getLong(String key) {
        String value = properties.getProperty(key);
        if (value != null) { return LangUtils.parseLong(value); }
        return 0;
    }
    
    public double getDouble(String key) {
        String value = properties.getProperty(key);
        if (value != null) { return LangUtils.parseDouble(value); }
        return 0.0;
    }
    
    public float getFloat(String key) {
        String value = properties.getProperty(key);
        if (value != null) { return LangUtils.parseFloat(value); }
        return 0;
    }
    
    public boolean getBoolean(String key) {
        String value = properties.getProperty(key);
        if (value != null) { return LangUtils.parseBoolean(value); }
        return false;
    }
    
    public boolean containsKey(Object key) {
        return properties.containsKey(key);
    }
    
    public boolean containsKey(String key) {
        return properties.containsKey(key);
    }
    
    public Properties getProperties() {
        return this.properties;
    }
    
}
